Skip to main content

jq

Introduction

This cheat sheet provides a quick reference for some common jq commands and concepts. jq is a command-line utility used for processing and manipulating JSON data.

jq Concepts

Selecting Data

jq allows you to select specific data from JSON.

  • Select a specific field:

    jq '.field_name' input.json
  • Select nested fields:

    jq '.parent_field.child_field' input.json

Filtering Data

You can filter JSON data based on conditions.

  • Filter data by a specific condition:

    jq '.[] | select(.field_name == "value")' input.json
  • Filter data by array elements:

    jq '.array_field[] | select(.field_name == "value")' input.json

Modifying Data

jq allows you to modify JSON data.

  • Update a field:

    jq '.field_name = "new_value"' input.json
  • Add a new field:

    jq '.new_field = "value"' input.json

Arrays

You can work with arrays in JSON.

  • Get the length of an array:

    jq 'length' input.json
  • Iterate over array elements:

    jq '.[]' input.json

Pipes

You can pipe multiple jq operations together.

  • Chain multiple operations:
    jq '.field | .[] | select(.value > 10)' input.json

jq Command-Line

  • Select a specific field from JSON:

    jq '.field_name' input.json
  • Filter data by a condition:

    jq '.[] | select(.field_name == "value")' input.json
  • Update a field in JSON:

    jq '.field_name = "new_value"' input.json
  • Get the length of an array in JSON:

    jq 'length' input.json
  • Pipe multiple jq operations together:

    jq '.field | .[] | select(.value > 10)' input.json

Conclusion

This cheat sheet covers some common jq commands and concepts. jq is a versatile tool for processing and manipulating JSON data, making it useful for tasks like data extraction, filtering, and transformation; refer to the official jq documentation for more in-depth information and advanced usage.